/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dorian.atomicuml.dao;

import com.dorian.atomicuml.dao.interfaces.UserManager;
import com.dorian.atomicuml.orm.Users;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.dao.DataAccessException;

/**
 *
 * @author cedric.creusot
 */
public class UserManagerImpl implements UserManager {

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public void createUser(String firstName,
                           String lastName,
                           String password,
                           String mail) throws Exception {
        
        if (this.getUser(mail) != null)
            throw new Exception("User already exist");
        
        Session session = sessionFactory.getCurrentSession();
        
        Users u = new Users(0,
                          firstName,
                          lastName,
                          mail,
                          password,
                          false);
        session.save(u);
    }

    @Override
    public void updateUser(Users user) {
        Session session = sessionFactory.getCurrentSession();
        
        session.saveOrUpdate(user);
    }
    

    @Override
    public boolean deleteUser(int userId) throws Exception {
        Session session = sessionFactory.getCurrentSession();
        
        Query query = session.getNamedQuery("USERS.findById")
                             .setParameter("id", userId);
                        
        Users u = (Users) query.uniqueResult();
        try
        {
        session.delete(u);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean deleteUser(Users user) throws Exception {
        try
        {
            Session session = sessionFactory.getCurrentSession();
            session.delete(user);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public List<Users> getAllUsers() {
        Session session = sessionFactory.getCurrentSession();
        
        Query query = session.createSQLQuery("select * from USERS")
                             .addEntity(Users.class);
        return query.list();
    }

    @Override
    public Users getUser(String mail) throws Exception {
        if (mail == null)
            throw new IllegalArgumentException("Login is empty.");
        try
        {
            
            System.out.println("Creation session hibernate.");
            Session session = sessionFactory.getCurrentSession();
           
            Query query = session.getNamedQuery("USERS.findByMail")
                                 .setParameter("mail", mail);
            Users u = (Users)query.uniqueResult();
            return u;
        }
        catch (DataAccessException e)
        {
            System.out.println("Exception : \n" + e);
            return null;
        }
    }

    @Override
    public boolean checkLogin(String mail, String password) throws Exception {
        Session session = sessionFactory.getCurrentSession();
        
        Query query = session.getNamedQuery("USERS.findByMailAndPassword")
                             .setParameter("mail", mail)
                             .setParameter("password", password);
        return (query.uniqueResult() != null);
    }

    @Override
    public Users getUser(int id) {
        Session session = sessionFactory.getCurrentSession();
        
        Query query = session.getNamedQuery("USERS.findById")
                             .setParameter("id", id);
        return ((Users)query.uniqueResult());
    }


}
